package club.kunlingou.dsc.druid.configuration;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSourceFactory;

@Configuration
public class DruidConfiguration {
	
	@Autowired
	Environment env;
	
	@Bean
	public DataSource dataSource() {
		Properties info = new Properties();
		info.setProperty("driverClassName", env.getProperty("spring.datasource.driverClassName"));
		info.setProperty("url", env.getProperty("spring.datasource.url"));
		info.setProperty("username", env.getProperty("spring.datasource.username"));
		info.setProperty("password", env.getProperty("spring.datasource.password"));
		info.setProperty("initialSize", "5");
		info.setProperty("maxActive", "10");
		info.setProperty("maxWait", "3000");
		info.setProperty("maxIdle", "6");
		info.setProperty("minIdle", "3");
		try {
			return DruidDataSourceFactory.createDataSource(info);
		} catch (Exception e) {
			e.printStackTrace();
		};
		return null;
	}
	
	@Bean
	public JdbcTemplate jdbcTemplate(DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}
	
}
